package com.fwtai.pay.wechat;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;

import java.io.InputStream;
import java.net.URI;
import java.net.URLConnection;

import static org.apache.http.HttpHeaders.ACCEPT;
import static org.apache.http.entity.ContentType.APPLICATION_JSON;

public class WechatPayUploadHttpPost extends HttpPost {

  private final String meta;

  private WechatPayUploadHttpPost(final URI uri,final String meta) {
    super(uri);
    this.meta = meta;
  }

  public String getMeta() {
    return meta;
  }

  public static class Builder {

    private final URI uri;
    private String fileName;
    private InputStream fileInputStream;
    private ContentType fileContentType;
    private String meta;

    public Builder(final URI uri) {
      if (uri == null) {
        throw new IllegalArgumentException("上传文件接口URL为空");
      }
      this.uri = uri;
    }

    public Builder withImage(final String fileName,final String fileSha256,final InputStream inputStream) {
      if (fileSha256 == null || fileSha256.isEmpty()) {
        throw new IllegalArgumentException("文件摘要为空");
      }
      meta = String.format("{\"filename\":\"%s\",\"sha256\":\"%s\"}", fileName, fileSha256);
      return withFile(fileName, meta, inputStream);
    }

    public Builder withFile(final String fileName,final String meta,final InputStream inputStream) {
      this.fileName = fileName;
      this.fileInputStream = inputStream;
      final String mimeType = URLConnection.guessContentTypeFromName(fileName);
      if (mimeType == null) {
        // guess this is a video uploading
        this.fileContentType = ContentType.APPLICATION_OCTET_STREAM;
      } else {
        this.fileContentType = ContentType.create(mimeType);
      }
      this.meta = meta;
      return this;
    }

    //文件上传???
    public WechatPayUploadHttpPost build(){
      if (fileName == null || fileName.isEmpty()) {
        throw new IllegalArgumentException("文件名称为空");
      }
      if (fileInputStream == null) {
        throw new IllegalArgumentException("文件为空");
      }
      if (fileContentType == null) {
        throw new IllegalArgumentException("文件类型为空");
      }
      if (meta == null || meta.isEmpty()) {
        throw new IllegalArgumentException("媒体文件元信息为空");
      }
      final WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta);
      final MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
      entityBuilder.setMode(HttpMultipartMode.RFC6532).addTextBody("meta", meta, APPLICATION_JSON).addBinaryBody("file",fileInputStream,fileContentType,fileName);
      HttpEntity entity = entityBuilder.build();
      request.setEntity(entity);
      request.addHeader(ACCEPT, APPLICATION_JSON.toString());
      return request;
    }
  }
}